//
//  AppDelegate.m
//  WindRunner
//
//  Created by leeqb on 9/9/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseTabBarController.h"
#import "UMSocialFacebookHandler.h"
#import "UMSocialSinaHandler.h"

#import "AppDelegate+EaseMob.h"
#import "AppDelegate+MagicalRecord.h"

@implementation AppDelegate

// 禁止横屏
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    _tabBar = [[BaseTabBarController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_tabBar];
    navController.navigationBarHidden = YES;
    self.window.rootViewController = navController;
    [navController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"nav_back.png"]];
    
    /*UINavigationController *nav = (UINavigationController *)_tabBar.viewControllers[3];
    nav.tabBarItem.badgeValue = @"10";*/
    
    // 初始化第三方库
    [self initThird:application launchOptions:launchOptions];
    
    // 初始化定位器
    [LocationHelper initLocationManager];
    
    // 以下目录不会被iCloud备份
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()]];
    [self addSkipBackupAttributeToItemAtURL:url];
    
    url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Library", NSHomeDirectory()]];
    [self addSkipBackupAttributeToItemAtURL:url];
    
    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
    
    // ----------------- 环信初始化Start -----------------
    [self setupUIDemoDB];
    [self easemobApplication:application didFinishLaunchingWithOptions:launchOptions];
    // ----------------- 环信初始化End -----------------
    
    return YES;
}

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
    
    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

- (void)initThird:(UIApplication *)application launchOptions:(NSDictionary *)launchOptions
{
    // 初始化属性列表
    [FMPropertyHelper initPropertyList];
    
    // 设置友盟key
    [MobClick startWithAppkey:@"541a8facfd98c5aefa03486d"];
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];
    
    // 意见反馈
    [UMFeedback setAppkey:@"541a8facfd98c5aefa03486d"];
    
    // 初始化友盟社会化组件
    [UMSocialData setAppKey:@"51905b2556240bb21e02c115"];
    
    // Facebook
    [UMSocialFacebookHandler setFacebookAppID:@"599169466859087" shareFacebookWithURL:@"http://www.umeng.com/social"];
    
    // 初始化新浪微博
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    // 初始化Instagram
    self.instagram = [[Instagram alloc] initWithClientId:@"fd725621c5e44198a5b8ad3f7a0ffa09" delegate:nil];
}

void MySignalHandler(int signal)
{
    NSLog(@"+++++++++========+++++++++++:%d", signal);
}

void UncaughtExceptionHandler(NSException *exception) {
    signal(SIGABRT, MySignalHandler);
    signal(SIGILL, MySignalHandler);
    signal(SIGSEGV, MySignalHandler);
    signal(SIGFPE, MySignalHandler);
    signal(SIGBUS, MySignalHandler);
    signal(SIGPIPE, MySignalHandler);
    
    NSArray *arr = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *urlStr = [NSString stringWithFormat:@"mailto://dolphin2525@163.com?subject=bug报告&body=感谢您的配合!<br><br><br>"
                        "错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",
                        name,reason,[arr componentsJoinedByString:@"<br>"]];
    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:url];
    
    //或者直接用代码，输入这个崩溃信息，以便在console中进一步分析错误原因
    NSLog(@"1heqin, CRASH: %@", exception);
    NSLog(@"heqin, Stack Trace: %@", [exception callStackSymbols]);
}

@end
